package com.chuangyang.fixboxmaster.ui.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.R;


/**
 * Created by Administrator on 2015/4/30.
 */
public class ItemView extends LinearLayout{

    private ImageView leftItemIcon;
    private ImageView rightItemIcon;
    private TextView leftItemDescription;
    private TextView righttItemDescription;


    private void init(){
        setClickable(true);
        setFocusable(true);
    }


    private void drawView(Context context,AttributeSet attrs){
        TypedArray a = null;
        View view ;
        try {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.pay_list_item, this, true);
            leftItemIcon = (ImageView) view.findViewById(R.id.item_left_icon);
            rightItemIcon = (ImageView) view.findViewById(R.id.item_right_icon);
            leftItemDescription = (TextView) view.findViewById(R.id.item_left_des);
            righttItemDescription = (TextView) view.findViewById(R.id.item_right_des);
            a = context.obtainStyledAttributes(attrs, R.styleable.ItemView);
            Drawable leftsrc = a.getDrawable(R.styleable.ItemView_left_src1);
            Drawable righttsrc = a.getDrawable(R.styleable.ItemView_right_src1);
            String leftdescription=a.getString(R.styleable.ItemView_left_description1);
            String rightdescription=a.getString(R.styleable.ItemView_right_description1);
            if(leftsrc!=null){
                leftItemIcon.setVisibility(VISIBLE);
                leftItemIcon.setImageDrawable(leftsrc);
            }
            if(righttsrc!=null){
                rightItemIcon.setVisibility(VISIBLE);
                rightItemIcon.setImageDrawable(righttsrc);
            }
            setLeftItemDescription(leftdescription);
            setRightItemDescription(rightdescription);

        } catch (Exception e) {
        } finally {
            if (a != null)
                a.recycle();
        }
    }

    public void setRightItemDescription(String desc){
        if (desc != null)
        {
            righttItemDescription.setVisibility(VISIBLE);
            righttItemDescription.setText(desc);
        }
    }
    public void setRightItemDescriptionColor(int color){
        if (righttItemDescription != null)
        {
            righttItemDescription.setVisibility(VISIBLE);
            righttItemDescription.setTextColor(color);
        }
    }
    public void setLeftItemDescription(String desc){
        if (desc != null)
        {
            leftItemDescription.setVisibility(VISIBLE);
            leftItemDescription.setText(desc);
        }
    }


    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        drawView(context,attrs);
    }
}
